﻿using UnityEngine;
using System.Collections;

public class GUIcounter : MonoBehaviour 
{
//	Texture2D TextTexture;			//this will be the color of the numbers displaying the counter
	Texture2D ItemSymbolTexture;	//this image will show a representation of what the counter is for (i.e bullets)
	public Vector2 screenPosition;			//this will offset the gui bar from the top left corner
	public Vector2 displaySize;			//this vector will determine the width and height of the box

	public bool isEnabled = true;
	int countNumber = 0;

	public void SetCounterNumber(int num)
	{
		if(num < 0)
		{
			Debug.Log("ERROR: number must be positive");
			Mathf.Clamp(num, 0, 1000);
		}
		countNumber = num;
	}
	
	public void SetSymbolTexture(Texture2D tex) { ItemSymbolTexture = tex; }
	public void SetPosition(Vector2 pos) { screenPosition = pos; }
	public void SetSize(Vector2 size)
	{
		//boarder thickness is between 0-1 and this will clamp it to make sure
		if(size.x < 0)
		{
			Debug.Log("ERROR: sizes must be between 0 - 100");
			Mathf.Clamp(size.x, 0, 100.0f);
		}
		else if(size.y < 0)
		{
			Debug.Log("ERROR: sizes must be between 0 - 100");
			Mathf.Clamp(size.y, 0, 100.0f);
		}
		
		displaySize = size;
	}
	
	//set up any values that have not been pre set
	void Start()
	{
		if(ItemSymbolTexture == null)
		{
			ItemSymbolTexture = new Texture2D (1, 1);
			ItemSymbolTexture.SetPixel(0,0,Color.white);
			ItemSymbolTexture.Apply();
		}

		//set Default Location
		if(screenPosition == Vector2.zero)
		{
			screenPosition = new Vector2 (2, 2);
		}

		//set Default Location
		if(displaySize == Vector2.zero)
		{
			displaySize = new Vector2 (2, 2);
		}
	}
	
	//This function will display the bar on screen
	void OnGUI () 
	{

		//these numbers make it easier to adjust the values from the inspector. This way the user can change between 0-100 instead of 0-1
		float sWidth = Screen.width / 100.0f;
		float sHeight = Screen.height / 100.0f;
		
		//make variables to simplify the rect creation
		float xOffset = screenPosition.x * sWidth;
		float yOffset = screenPosition.y * sHeight;
		float height = displaySize.y * sHeight;
		float TextWidth = displaySize.x * sWidth;
		float symbolTextOffset = height / 10.0f;

		//make rects to draw to
		Rect symbolRect = new Rect (xOffset, yOffset, height , height);
		Rect textRect = new Rect (xOffset + height + symbolTextOffset, yOffset , TextWidth, height );

		//draw the bar to the screen
		GUI.DrawTexture( symbolRect, ItemSymbolTexture);
		GUI.TextArea( textRect, countNumber.ToString());
	}
}
